//
//  ZWY_NetHandle.m
//  EDPS
//
//  Created by 张维扬 on 15/11/23.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "ZWY_NetHandle.h"

@implementation ZWY_NetHandle
+ (void)getURL:(NSString *)url Cookie:(NSString *)cookie parameter:(NSString *)parameter result:(JSONBLOCK)block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (cookie) {
        
        [manager.requestSerializer setValue:@"BreadTrip/6.1.1/zh (iPhone7,2; iPhone OS9.1; zh-Hans-CN zh_CN) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    }
    if (parameter) {
        [manager.requestSerializer setValue:parameter forHTTPHeaderField:@"HTTP-AUTHORIZATION"];
    }
    
    //    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    // 返回数据的格式
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/json",@"text/html",@"application/json",@"text/plain",@"text/javascript", nil]];
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // 如果请求成功, 回调 请求到的数据, 同时 在这里  做本地缓存
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        // 在这里读取本地缓存
        NSLog(@"%@", error);
    }];
}

+ (void)postURL:(NSString *)url parameter:(id)parameter Cookie:(NSString *)cookie result:(JSONBLOCK)block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (cookie) {
        [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"cookie"];
    }
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/json",@"text/html",@"application/json",@"text/plain",@"text/javascript", nil]];
    [manager POST:url parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

@end
